#!/usr/bin/env python

from mininet.topo import Topo
from mininet.net import Mininet
from mininet.node import OVSKernelSwitch, RemoteController
from mininet.cli import CLI
from mininet.link import TCLink
from mininet.log import setLogLevel,info
import os

def myNetwork():
    net=Mininet(controller=RemoteController, switch=OVSKernelSwitch)
    c0=net.addController(name='c0',controller=RemoteController,port=6633)
    s1 = net.addSwitch('s1')
    s2 = net.addSwitch('s2')
    s3 =net.addSwitch('s3')

    h1 = net.addHost('h1', ip='10.0.0.1/24',mac='00:00:00:00:01:01')
    h2 = net.addHost('h2', ip='10.0.0.2/24',mac='00:00:00:00:01:02')
    h3 = net.addHost('h3', ip='10.0.0.3/24',mac='00:00:00:00:02:01')
    h4 = net.addHost('h4', ip='10.0.0.4/24',mac='00:00:00:00:02:02')

    net.addLink(h1, s1,port2=3)
    net.addLink(h2, s3,port2=4)
    net.addLink(h3, s1,port2=4)
    net.addLink(h4, s3,port2=3)
    net.addLink(s1, s3,port1=1,port2=1)
    net.addLink(s1, s2,port1=2)
    net.addLink(s3, s2,port1=2)
    
    net.build()
    c0.start()
    s1.start([c0])
    s2.start([c0])
    s3.start([c0])
    
    s1.cmd('ifconfig s1-eth1 192.168.1.1/24')
    s2.cmd('ifconfig s2-eth1 192.168.1.2/24')
    s3.cmd('ifconfig s2-eth1 192.168.1.3/24')

    #注意S1 S3上的隧道名称不能重名！！！一定不能重名！！！
    s1.cmd('ovs-vsctl add-port s1 vxlan100 -- set interface vxlan100 type=vxlan \
        options:remote_ip=192.168.1.3 options:local_ip=192.168.1.1 options:key=100')
    s1.cmd('ovs-vsctl add-port s1 vxlan200 -- set interface vxlan200 type=vxlan \
        options:remote_ip=192.168.1.3 options:local_ip=192.168.1.1 options:key=200')
    
    s3.cmd('ovs-vsctl add-port s3 vxlan300 -- set interface vxlan300 type=vxlan \
        options:remote_ip=192.168.1.1 options:local_ip=192.168.1.3 options:key=100')    
    s3.cmd('ovs-vsctl add-port s3 vxlan400 -- set interface vxlan400 type=vxlan \
        options:remote_ip=192.168.1.1 options:local_ip=192.168.1.3 options:key=200')

    #s1.cmd('ovs-ofctl del-flows s1')
    #s3.cmd('ovs-ofctl del-flows s3')
    
    # s1.cmd('ovs-ofctl add-flow s1 "table=0,priority=1,in_port=5 actions=output:3"')
    # s1.cmd('ovs-ofctl add-flow s1 "table=0,priority=1,in_port=3 actions=output:5"')
    # s1.cmd('ovs-ofctl add-flow s1 "table=0,priority=1,in_port=4 actions=output:6"')
    # s1.cmd('ovs-ofctl add-flow s1 "table=0,priority=1,in_port=6 actions=output:4"')
    # s3.cmd('ovs-ofctl add-flow s3 "table=0,priority=1,in_port=4 actions=output:5"')
    # s3.cmd('ovs-ofctl add-flow s3 "table=0,priority=1,in_port=5 actions=output:4"')
    # s3.cmd('ovs-ofctl add-flow s3 "table=0,priority=1,in_port=6 actions=output:3"')
    # s3.cmd('ovs-ofctl add-flow s3 "table=0,priority=1,in_port=3 actions=output:6"')

    CLI(net)
    net.stop()

if __name__=='__main__':
    setLogLevel('info')
    myNetwork()
    
    
# def configure_vxlan(net):
    
    # s1,s2,s3 = net.get('s1', 's2','s3')
    
    # s1.cmd('ifconfig s1-eth1 192.168.1.1/24')
    # s2.cmd('ifconfig s2-eth1 192.168.1.2/24')
    # s3.cmd('ifconfig s2-eth1 192.168.1.3/24')

    # #注意S1 S3上的隧道名称不能重名！！！一定不能重名！！！
    # s1.cmd('ovs-vsctl add-port s1 vxlan100 -- set interface vxlan100 type=vxlan \
    #        options:remote_ip=192.168.1.3 options:local_ip=192.168.1.1 options:key=100')
    # s1.cmd('ovs-vsctl add-port s1 vxlan200 -- set interface vxlan200 type=vxlan \
    #        options:remote_ip=192.168.1.3 options:local_ip=192.168.1.1 options:key=200')
    
    # s3.cmd('ovs-vsctl add-port s3 vxlan300 -- set interface vxlan300 type=vxlan \
    #        options:remote_ip=192.168.1.1 options:local_ip=192.168.1.3 options:key=100')    
    # s3.cmd('ovs-vsctl add-port s3 vxlan400 -- set interface vxlan400 type=vxlan \
    #        options:remote_ip=192.168.1.1 options:local_ip=192.168.1.3 options:key=200')

    # s1.cmd('ovs-ofctl del-flows s1')
    # s3.cmd('ovs-ofctl del-flows s3')
    
    


    #匹配项加上mac地址似乎不行，因为没有广播机制，泛洪数据包不可达
    # s1.cmd('ovs-ofctl add-flow s1 "table=0,priority=1,in_port=5,dl_dst=00:00:00:00:01:01 actions=output:3"')
    # s1.cmd('ovs-ofctl add-flow s1 "table=0,priority=1,in_port=3,dl_dst=00:00:00:00:01:02 actions=output:5"')
    #s1.cmd('ovs-ofctl add-flow s1 "table=0,priority=1,in_port=4,dl_dst=00:00:00:00:02:02 actions=output:6"')
    #s1.cmd('ovs-ofctl add-flow s1 "table=0,priority=1,in_port=6,dl_dst=00:00:00:00:02:01 actions=output:4"')
    
    # s3.cmd('ovs-ofctl add-flow s3 "table=0,priority=1,in_port=4,dl_dst=00:00:00:00:01:01 actions=output:5"')
    # s3.cmd('ovs-ofctl add-flow s3 "table=0,priority=1,in_port=5,dl_dst=00:00:00:00:01:02 actions=output:4"')
    # s3.cmd('ovs-ofctl add-flow s3 "table=0,priority=1,in_port=6,dl_dst=00:00:00:00:02:02 actions=output:3"')
    # s3.cmd('ovs-ofctl add-flow s3 "table=0,priority=1,in_port=3,dl_dst=00:00:00:00:02:01 actions=output:6"')
    
    # s1.cmd('ovs-ofctl add-flow s1 "table=0,priority=1,in_port=5 actions=output:3"')
    # s1.cmd('ovs-ofctl add-flow s1 "table=0,priority=1,in_port=3 actions=output:5"')
    # s1.cmd('ovs-ofctl add-flow s1 "table=0,priority=1,in_port=4 actions=output:6"')
    # s1.cmd('ovs-ofctl add-flow s1 "table=0,priority=1,in_port=6 actions=output:4"')
    # s3.cmd('ovs-ofctl add-flow s3 "table=0,priority=1,in_port=4 actions=output:5"')
    # s3.cmd('ovs-ofctl add-flow s3 "table=0,priority=1,in_port=5 actions=output:4"')
    # s3.cmd('ovs-ofctl add-flow s3 "table=0,priority=1,in_port=6 actions=output:3"')
    # s3.cmd('ovs-ofctl add-flow s3 "table=0,priority=1,in_port=3 actions=output:6"')
    

    # s1.cmd('ovs-ofctl add-flow s1 "table=0,priority=1,dl_dst=ff:ff:ff:ff:ff:ff actions=ALL"')
    # s1.cmd('ovs-ofctl add-flow s1 "table=0,priority=1 actions=NORMAL"')
    # s3.cmd('ovs-ofctl add-flow s3 "table=0,priority=1,dl_dst=ff:ff:ff:ff:ff:ff actions=ALL"')
    # s3.cmd('ovs-ofctl add-flow s3 "table=0,priority=1 actions=NORMAL"')

